#include<iostream>
#include<unordered_map>
using namespace std;

class NodeInfo
{
public:
    int val;
    int left;
    int right;
    NodeInfo(int cval,int cleft,int cright)
    {
        val = cval;
        left = cleft;
        right = cright;
    }
};

int findSuccessorNode(int val,const unordered_map<int,NodeInfo>& fmap,const unordered_map<int,int>& pmap);

int main(){
    unordered_map<int,NodeInfo> fmap;
    unordered_map<int,int> pmap;
    int n,rval;
    cin >> n >> rval;
    for(int i=0; i<n; i++){
        int cval,cleft,cright;
        cin >> cval >> cleft >> cright;
        fmap.insert(pair<int,NodeInfo>(cval,NodeInfo(cval,cleft,cright)));
        pmap.insert(pair<int,int>(cright,cval));
        pmap.insert(pair<int,int>(cleft,cval));
    }
    pmap.insert(pair<int,int>(rval,0));


    // cout << "len:" << fmap.size() << " " << pmap.size()<< endl;

    int fval;
    cin >> fval;
    cout << findSuccessorNode(fval,fmap,pmap) << endl;
    return 0;
}

int findSuccessorNode(int val,const unordered_map<int,NodeInfo>& fmap,const unordered_map<int,int>& pmap)
{
    if(val == 0){
        return 0;
    }
    int rightTree = fmap.find(val)->second.right;
    if(rightTree != 0){
        //返回右树最左子
        int curVal = rightTree;
        int curValLeft = fmap.find(curVal)->second.left;
        while(curValLeft != 0){
            curVal = curValLeft;
            curValLeft = fmap.find(curVal)->second.left;
        }
        return curVal;
    }
    //无右树 寻找为左子的节点
    int curVal = val;
    int curValParent = pmap.find(curVal)->second;
    //不是根节点 且当前节点不是父节点的左孩子 继续寻找
    while(curValParent != 0 && curVal != fmap.find(curValParent)->second.left){
        curVal = curValParent;
        curValParent = pmap.find(curVal)->second;
    }
    return curValParent;
}